﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace CustomControls
{
    [DefaultEvent("ItemMoved")]
    public class DualListBoxControl : Control
    {
        #region 私有控件
        private ListBox sourceListBox;
        private ListBox targetListBox;
        private Button addButton;
        private Button removeButton;
        private Button addAllButton;
        private Button removeAllButton;
        private TableLayoutPanel mainLayout;
        #endregion

        #region 构造函数
        public DualListBoxControl()
        {
            InitializeComponents();
            InitializeLayout();
            InitializeEvents();

            // 设置控件默认属性
            Size = new Size(400, 300);
            DoubleBuffered = true;
        }
        #endregion

        #region 初始化方法
        private void InitializeComponents()
        {
            // 源ListBox
            sourceListBox = new ListBox
            {
                Dock = DockStyle.Fill,
                SelectionMode = SelectionMode.MultiExtended
            };

            // 目标ListBox
            targetListBox = new ListBox
            {
                Dock = DockStyle.Fill,
                SelectionMode = SelectionMode.MultiExtended
            };

            // 添加按钮
            addButton = new Button
            {
                Text = ">>",
                Dock = DockStyle.Top,
                Margin = new Padding(3, 0, 3, 5)
            };

            // 移除按钮
            removeButton = new Button
            {
                Text = "<<",
                Dock = DockStyle.Top,
                Margin = new Padding(3, 0, 3, 5)
            };

            // 添加所有按钮
            addAllButton = new Button
            {
                Text = ">>>",
                Dock = DockStyle.Top,
                Margin = new Padding(3, 0, 3, 5)
            };

            // 移除所有按钮
            removeAllButton = new Button
            {
                Text = "<<<",
                Dock = DockStyle.Top,
                Margin = new Padding(3, 0, 3, 5)
            };

            // 主布局
            mainLayout = new TableLayoutPanel
            {
                Dock = DockStyle.Fill,
                ColumnCount = 3,
                RowCount = 1,
                ColumnStyles =
                {
                    new ColumnStyle(SizeType.Percent, 40F),
                    new ColumnStyle(SizeType.Percent, 20F),
                    new ColumnStyle(SizeType.Percent, 40F)
                },
                RowStyles = { new RowStyle(SizeType.Percent, 100F) }
            };
        }

        private void InitializeLayout()
        {
            // 创建按钮面板
            Panel buttonPanel = new Panel
            {
                Dock = DockStyle.Fill,
                Padding = new Padding(5, 30, 5, 30)
            };

            // 将按钮添加到按钮面板
            buttonPanel.Controls.Add(addAllButton);
            buttonPanel.Controls.Add(addButton);
            buttonPanel.Controls.Add(removeButton);
            buttonPanel.Controls.Add(removeAllButton);

            // 将控件添加到主布局
            mainLayout.Controls.Add(sourceListBox, 0, 0);
            mainLayout.Controls.Add(buttonPanel, 1, 0);
            mainLayout.Controls.Add(targetListBox, 2, 0);

            // 将主布局添加到控件
            Controls.Add(mainLayout);
        }

        private void InitializeEvents()
        {
            addButton.Click += AddButton_Click;
            removeButton.Click += RemoveButton_Click;
            addAllButton.Click += AddAllButton_Click;
            removeAllButton.Click += RemoveAllButton_Click;
        }
        #endregion

        #region 事件处理
        private void AddButton_Click(object sender, EventArgs e)
        {
            MoveSelectedItems(sourceListBox, targetListBox);
        }

        private void RemoveButton_Click(object sender, EventArgs e)
        {
            MoveSelectedItems(targetListBox, sourceListBox);
        }

        private void AddAllButton_Click(object sender, EventArgs e)
        {
            MoveAllItems(sourceListBox, targetListBox);
        }

        private void RemoveAllButton_Click(object sender, EventArgs e)
        {
            MoveAllItems(targetListBox, sourceListBox);
        }

        private void MoveSelectedItems(ListBox source, ListBox target)
        {
            ArrayList tempList = new ArrayList();

            // 复制选中项到临时列表
            foreach (object item in source.SelectedItems)
            {
                tempList.Add(item);
            }

            // 从源ListBox移除选中项
            foreach (object item in tempList)
            {
                source.Items.Remove(item);
            }

            // 添加到目标ListBox
            target.Items.AddRange(tempList.ToArray());

            // 触发事件
            OnItemMoved(new ItemMovedEventArgs(tempList, source == sourceListBox));
        }

        private void MoveAllItems(ListBox source, ListBox target)
        {
            ArrayList tempList = new ArrayList();

            // 复制所有项到临时列表
            foreach (object item in source.Items)
            {
                tempList.Add(item);
            }

            // 清空源ListBox
            source.Items.Clear();

            // 添加到目标ListBox
            target.Items.AddRange(tempList.ToArray());

            // 触发事件
            OnItemMoved(new ItemMovedEventArgs(tempList, source == sourceListBox));
        }
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取源ListBox
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ListBox SourceListBox => sourceListBox;

        /// <summary>
        /// 获取目标ListBox
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ListBox TargetListBox => targetListBox;

        /// <summary>
        /// 获取或设置添加按钮文本
        /// </summary>
        [Category("Appearance")]
        [Description("添加按钮文本")]
        public string AddButtonText
        {
            get => addButton.Text;
            set => addButton.Text = value;
        }

        /// <summary>
        /// 获取或设置移除按钮文本
        /// </summary>
        [Category("Appearance")]
        [Description("移除按钮文本")]
        public string RemoveButtonText
        {
            get => removeButton.Text;
            set => removeButton.Text = value;
        }
        #endregion

        #region 事件定义
        /// <summary>
        /// 项目移动事件
        /// </summary>
        public event EventHandler<ItemMovedEventArgs> ItemMoved;

        /// <summary>
        /// 触发ItemMoved事件
        /// </summary>
        protected virtual void OnItemMoved(ItemMovedEventArgs e)
        {
            ItemMoved?.Invoke(this, e);
        }
        #endregion

        #region 自定义事件参数类
        /// <summary>
        /// 项目移动事件参数
        /// </summary>
        public class ItemMovedEventArgs : EventArgs
        {
            public ArrayList Items { get; }
            public bool IsFromSource { get; }

            public ItemMovedEventArgs(ArrayList items, bool isFromSource)
            {
                Items = items;
                IsFromSource = isFromSource;
            }
        }
        #endregion
    }
}